package com.asion.vmovie.activities;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.asion.vmovie.R;
import com.asion.vmovie.beans.CateDetials;
import com.asion.vmovie.ui.MVPContract.VideoContract;
import com.asion.vmovie.ui.MVPModel.VideoModel;
import com.asion.vmovie.ui.MVPPresenter.VideoPresenter;
import com.asion.vmovie.utils.ToastUtil;
import com.bumptech.glide.Glide;
import com.superplayer.library.SuperPlayer;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by asion on 16-10-21.
 */

public class VideoActivity extends BaseActivity<VideoPresenter, VideoModel> implements VideoContract.View, SuperPlayer.OnNetChangeListener {
    @BindView(R.id.view_super_player)
    SuperPlayer viewSuperPlayer;
    @BindView(R.id.video_back)
    ImageView videoBack;
    @BindView(R.id.video_share)
    ImageView videoShare;
    @BindView(R.id.video_title)
    TextView videoTitle;
    @BindView(R.id.video_subtitle)
    TextView videoSubtitle;
    @BindView(R.id.video_cate)
    TextView videoCate;
    @BindView(R.id.video_intro)
    TextView videoIntro;
    @BindView(R.id.video_image)
    ImageView videoImage;
    @BindView(R.id.web_likecount)
    TextView webLikecount;
    @BindView(R.id.web_like)
    LinearLayout webLike;
    @BindView(R.id.web_sharecount)
    TextView webSharecount;
    @BindView(R.id.web_share)
    LinearLayout webShare;
    @BindView(R.id.web_commentcount)
    TextView webCommentcount;
    @BindView(R.id.web_comment)
    LinearLayout webComment;
    @BindView(R.id.web_bottom)
    LinearLayout webBottom;

    private boolean isLive = false;

    public static void showAction(Activity activity, Bundle bundle) {
        Intent intent = new Intent(activity, VideoActivity.class);
        intent.putExtra("bundle", bundle);
        activity.startActivity(intent);
        activity.overridePendingTransition(R.anim.fade_in,
                R.anim.fade_out);
    }

    @Override
    public int getLayoutId() {
        return R.layout.video_activity;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    public void initView() {
        startProgressDialog();
        bottomSheetDialog = new BottomSheetDialog(this);
        Bundle bundle = getIntent().getBundleExtra("bundle");
        String postid = bundle.getString("postid");
        mPresenter.getCateDetials(Integer.parseInt(postid));

        videoBack.setOnClickListener(v -> finish());
    }

    @Override
    public void showDataSuccess(CateDetials cateDetials) {
        initPlayer(cateDetials.getData().getContent().getVideo().get(0).getQiniu_url());
        videoTitle.setText(cateDetials.getData().getTitle());
        videoSubtitle.setText(cateDetials.getData().getShare_sub_title());
        videoCate.setText(cateDetials.getData().getCate().get(0)+"/"+cateDetials.getData().getRating()+"分");
        videoIntro.setText(cateDetials.getData().getIntro());
        Glide.with(this).load(cateDetials.getData().getImage()).asBitmap().error(R.mipmap.default_error)
                .centerCrop().into(videoImage);
        webCommentcount.setText(cateDetials.getData().getCount_comment());
        webLikecount.setText(cateDetials.getData().getCount_like());
        webSharecount.setText(cateDetials.getData().getCount_share());
        stopProgressDialog();
    }

    @Override
    public void showDataError(Throwable throwable) {

    }

    private void initPlayer(String url) {
        if (isLive) {
            viewSuperPlayer.setLive(true);//设置该地址是直播的地址
        }
        viewSuperPlayer.setNetChangeListener(true)//设置监听手机网络的变化
                .setOnNetChangeListener(this)//实现网络变化的回调
                .play(url);//开始播放视频
        viewSuperPlayer.setScaleType(SuperPlayer.SCALETYPE_FITXY);
        viewSuperPlayer.setPlayerWH(0, viewSuperPlayer.getMeasuredHeight());//设置竖屏的时候屏幕的高度，如果不设置会切换后按照16:9的高度重置
    }

    @Override
    public void onWifi() {
        ToastUtil.showShort("当前网络环境是WIFI");
    }

    @Override
    public void onMobile() {
        ToastUtil.showShort("当前网络环境是手机网络");
    }

    @Override
    public void onDisConnect() {
        ToastUtil.showShort("网络链接断开");
    }

    @Override
    public void onNoAvailable() {
        ToastUtil.showShort("无网络链接");
    }


    /**
     * 下面的这几个Activity的生命状态很重要
     */
    @Override
    protected void onPause() {
        super.onPause();
        if (viewSuperPlayer != null) {
            viewSuperPlayer.onPause();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (viewSuperPlayer != null) {
            viewSuperPlayer.onResume();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (viewSuperPlayer != null) {
            viewSuperPlayer.onDestroy();
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (viewSuperPlayer != null) {
            viewSuperPlayer.onConfigurationChanged(newConfig);
        }
    }

    @Override
    public void onBackPressed() {
        if (viewSuperPlayer != null && viewSuperPlayer.onBackPressed()) {
            return;
        }
        super.onBackPressed();
    }

    private BottomSheetDialog bottomSheetDialog;
    @OnClick({R.id.video_share, R.id.web_share})
    public void onClick(View view) {
        View inflate = getLayoutInflater().inflate(R.layout.share_dialog, null);
        inflate.findViewById(R.id.dialog_close).setOnClickListener(v -> bottomSheetDialog.dismiss());
        inflate.findViewById(R.id.dialog_qq).setOnClickListener(v -> ToastUtil.showShort("qq"));
        inflate.findViewById(R.id.dialog_qzone).setOnClickListener(v -> ToastUtil.showShort("qzone"));
        inflate.findViewById(R.id.dialog_vblog).setOnClickListener(v -> ToastUtil.showShort("vblog"));
        inflate.findViewById(R.id.dialog_wechat).setOnClickListener(v -> ToastUtil.showShort("wechat"));
        bottomSheetDialog.setContentView(inflate);
        bottomSheetDialog.show();
    }
}
